<?php

define( 'DS' , DIRECTORY_SEPARATOR );
define( 'APP_ROOT' ,  dirname( dirname( __FILE__ )  ) . DS );

define( 'BLAB_ROOT' , dirname( __FILE__ ) . DS  );
define( 'LP3K_ROOT' , APP_ROOT.'lp3k'.DS  );

// include_once KIS_APP_ROOT.'ext_lib'. DS.'comm.function.php';
define( 'AROOT' , LP3K_ROOT.'php'.DS);
define( 'CROOT' , AROOT.'_lp'. DS.'core'. DS  );
require_once CROOT. 'lib'.DS.'core.function.php';

require_once 'config.php';
require_once 'incl/main.inc';

/**

*/

function init_room($uid){
    require_once BLAB_ROOT.'incl/rpg_infos.inc.php';
    // $max=mt_rand(6,32);
    $max=count($cityArr);
    if($max>24) $max=24;
    for ($i=0; $i < $max; $i++) { 
        $id=$i+1;

        $uuid=kis_uuid();
        $tmpSrc="INSERT INTO `chat_room`(`id`, `uuid`, `adm_uid`, `title`, `content`, `creator`, `modifier`, `create_date`, `modify_date`, `status_id`, `parent_id`) VALUES (null,?s,?s,?s,?s,?s,?s,?s,?s,?s,?s)";
        $intuid=intval($uid);
        $cf=date('Y-m-d H:i:s');
        $tmpSrc = prepare($tmpSrc,array($uuid,$intuid,$cityArr[$i],$titl,$intuid,$intuid,$cf,$cf,2,100));

        echo $tmpSrc.';'.PHP_EOL;
    }
}

/**

*/

dbconnect(); $settings=get_settings(0); $options=get_options(); 
// $lang=get_language(); 

unset($user);
if(isset($_COOKIE['blab8_xuidc'])){
	$uid=explode('z',$_COOKIE['blab8_xuidc']);

	if(isset($uid[1]) && hsh($uid[0].$settings['cookie_salt'])==$uid[1]){
		$uid=(int)$uid[0];

		$query='SELECT * FROM '.$dbss['prfx']."_users WHERE usr_id=$uid";
		$result=neutral_query($query);

		if(neutral_num_rows($result)>0){
			$ext_user=neutral_fetch_array($result);

			$user=array();
			$user['id']=(int)$ext_user['usr_id'];
			$user['name']=$ext_user['usr_name'];
			$user['mail']=$ext_user['usr_mail'];
			$user['status']=$ext_user['usr_status'];
			$user['room_id']=$ext_user['usr_room'];
		}
	}
}

if(!isset($user['id']) || !$user['name']){
	redirect('login.php');
	die();
}

$room_id=intval($user['room_id']);
$channel_id=0;

$a='adf403';
$tt = v('a') ;  if($tt) $a=$tt;
if(strlen($a)<=3){
    die('403 Access Denied/Forbidden');
}

switch ($a) {
case 'join':
	$rid_new=v('room_id');
	if($rid_new && intval($rid_new)>0 ){
		$rid_new=intval($rid_new);
		// UPDATE `blab8_users` SET `usr_id`=[value-1],`usr_name`=[value-2],`usr_pass`=[value-3],`usr_mail`=[value-4],`usr_join_date`=[value-5],`usr_status`=[value-6],`usr_room`=[value-7] WHERE 1
		$query='UPDATE '.$dbss['prfx'].'_users SET usr_room='.$rid_new.' WHERE usr_id='.$user['id'];
		neutral_query($query);
		
		redirect('wrpg_room.php?room_id='.$rid_new);
		die();
	}
	redirect('join_world.php');
	die();	
	break;

case 'init_room':
    init_room($user['id']);
    die();
    break;

default:
	die('403 Access Denied/Forbidden');
}


die('403 Access Denied/Forbidden #');

?>